<?php
/**
 * @version		$Id: banner.php 10878 2008-08-30 17:29:13Z willebil $
 * @package		Joomla
 * @subpackage	Banners
 * @copyright	Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
 * @license		GNU/GPL, see LICENSE.php
 * Joomla! is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 * See COPYRIGHT.php for copyright notices and details.
 */

// no direct access
defined ( '_JEXEC' ) or die ( 'Restricted access' );
jimport ( 'joomla.application.component.controller' );

class ManagerControllerUserPim extends JController {

	var $urldirect = "";
	var $mainframe = "";

	function __construct() {
		parent::__construct();

		$this->view = & $this->getView('userpim', 'html');
		$this->urldirect = 'index.php?option=com_manager&c=userpim';
		$this->mainframe = JFactory::getApplication();
	}

	function display() {
		$this->view->display();
	}

	function add() {
		ManagerHelper::showToolBarForm('Add New User');
		$this->view->addForm();
	}

	function edit() {
		ManagerHelper::showToolBarForm('Edit User');
		$this->view->addForm();
	}

	function cancel() {
		$this->mainframe->redirect($this->urldirect);
	}

	function save() {
		$post = JRequest::get('post', JREQUEST_ALLOWHTML);
		
		$id_user = intval($post['id']);
		if($id_user <= 0 ) {
			// new user	
			$randPass = JUserHelper::genRandomPassword();
			if($post['password'] !=""){
				$randPass=$post['password'];
			}
			
			$data = array(
				'name' => $post['name'],
				'username' => $post['email'],
				'email' => $post['email'],
				'groups' => array($post['group_id']),
				'block' => 0,
				'password' => $randPass, // set the password
				'password2' => $randPass, // confirm the password
				'sendEmail' => 1 // should the user receive system mails?
			);
			
			$user = ManagerHelper::createNewUser($data);
		}else{
			//update user
			if($post['password'] ==""){
				$data = array(
					'id' =>$id_user,
					'groups' => array($post['group_id']),
					'name' => $post['name'],
					'username' => $post['email'],
					'email' => $post['email'],
					'block' => 0,
					'sendEmail' => 1 // should the user receive system mails?
				);
				
			}else{
				$data = array(
					'id' =>$id_user,
					'name' => $post['name'],
					'username' => $post['email'],
					'email' => $post['email'],
					'groups' => array($post['group_id']),
					'block' => 0,
					'password' =>md5($post['password']), // set the password
					'password2' =>md5($post['password']), // confirm the password
					'sendEmail' => 1 // should the user receive system mails?
				);
				
			}
			
			$obj_user = JFactory::getUser($id_user); 
			$obj_user->setProperties($data);
			$obj_user->save(true);
			//var_dump($obj_user);die;
		}
		
		if($id_user > 0){
			$db =& JFactory::getDBO();
			$data =new stdClass();
			$data->group_id   = intval($post['group_id']);
			$data->user_id    = intval($id_user);
			//update group
			$query = 'UPDATE  #__user_usergroup_map SET group_id= ' . intval($post['group_id']);
	        $query.= ' WHERE user_id ='. $id_user;
	        $db->setQuery( $query );
	        $db->query();
		}
		
		$this->mainframe->redirect($this->urldirect);
	}
	
	function delete() {
		$db =& JFactory::getDBO();
		$cid = JRequest::getVar('cid');
		JArrayHelper::toInteger($cid);
		
		if(!empty($cid)) {
			$cids = implode( ',', $cid );
			$query = 'DELETE FROM #__users'
        			. ' WHERE id IN ( '. $cids .' )';
        	$db->setQuery( $query );
        	if ($db->query()) {
	        		$query = 'DELETE FROM #__user_usergroup_map'
	        			. ' WHERE id IN ( '. $cids .' )';
	        		$db->setQuery( $query );
	        		$db->query();
        		}
			
		}
		
		$this->mainframe->redirect($this->urldirect);
	}
}

?>